$(function(){
	$('#tt').datagrid({
		url:'/emailGroup/getEmailGroupListPage'
	});
	
});

function doAdd(){
	$('#addForm').form('clear');
	$('#addWin').window('open');
}

function doEdit(){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	$('#nameId').val(record.name);
	$('#rnumId').val(record.rnum);
	$('#remarkId').val(record.remark);
	$('#idId').val(record.id);
	
	$('#addWin').window('open');
}

function doDel(){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	$.messager.confirm('提示', '你确定要删除吗', function(opt){
		if (opt){
			var emailGroupId=record.id;
			$.ajax({
				url: '/emailGroup/deleteEmailGroup',
				type: 'POST',
				data : {
		            id:emailGroupId,
		            delete_flag:1
				},
				success : function(data) {
					if (data.return_code == '1') {
						$.messager.alert(market.content.title, "删除成功");
						$("#tt").datagrid('reload');
						$('#tt').datagrid('unselectAll');
					} else {
						$.messager.alert(market.content.title, data.return_msg);
					}
				},
				error : function(e) {
					$.messager.alert("未知错误");
				}
			})
		}
	});
}

function doSearch(){
	$('#tt').datagrid({
		url:'/emailGroup/getEmailGroupListPage',
		queryParams:{
		}
	});
	$('#tt').datagrid('unselectAll');
}

function save(){
	var id=$('#idId').val();
	var url='/emailGroup/updateEmailGroup';
	if(id==null||id==''){
		url='/emailGroup/saveEmailGroup';
	}
	
	$('#addForm').form('submit',{
		url : url,
		onSubmit : function() {
			var flag = $(this).form('validate');
			if (!flag) {
				return flag;
			}
			showProcess(true, market.content.title, market.content.submitting);
			return true;
		},
		success : function(data) {
			showProcess(false);
			var data = eval('(' + data + ')');
			if (data.return_code == '1') {
				$("#addWin").window('close');
				$('#tt').datagrid('reload');
				$('#tt').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		onLoadError : function() {
			showProcess(false);
			$.messager.alert(market.content.title, market.content.networkError);
		}
	});
	
}

function cancel(){
	$('#addWin').window('close');
}

function typeFormatter(value){
	if(value==1){
		return '是';
	}else{
		return '否';
	}
}

function doAddPeople(){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	var id=record.id;
	$('#emailWinId').window('open');
	

	$('#inPeopleId').datagrid({
		url:'/emailGroup/getInEmailListPage?emailGroupId='+id,
		queryParams:{
		}
	});
	
	$('#outPeopleId').datagrid({
		url:'/emailGroup/getOutEmailListPage?emailGroupId='+id,
		queryParams:{
		}
	});
	
	
}

function searchOutPeople(){
	var name=$('#outusernameId').val();
	var email=$('#outemailId').val();
	$('#outPeopleId').datagrid({
		queryParams:{
			userName:name,
			email:email
		}
	});
}

function searchInPeople(){
	var name=$('#inusernameId').val();
	var email=$('#inemailId').val();
	$('#inPeopleId').datagrid({
		queryParams:{
			userName:name,
			email:email
		}
	});
}

function outPeopleDblClickRow(rowIndex, rowData){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	reqAddOrMove(record.id,rowData.emailId,'/emailGroup/saveEmailGroupRef')
}
function inPeopleDblClickRow(rowIndex, rowData){
	var record = $('#tt').datagrid('getSelected');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	reqAddOrMove(record.id,rowData.emailId,'/emailGroup/deleteEmailGroupRef')
}


function outPeopleMove(){
	var gourp = $('#tt').datagrid('getSelected');
	if(gourp==null||gourp==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	var record = $('#outPeopleId').datagrid('getChecked');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var emailIdArr=[];
	for(var i=0;i<record.length;i++){
		emailIdArr[i]=record[i].emailId;
	}
	var emailIdStr=emailIdArr.join(",");
	var emaigroupId=gourp.id;
	reqAddOrMoveArr(emaigroupId,emailIdStr,'/emailGroup/saveEmailGroupRef');
}

function inPeopleMove(){
	var gourp = $('#tt').datagrid('getSelected');
	if(gourp==null||gourp==''){
		$.messager.alert("提示", "请先选择一条记录");
		return;
	}
	
	var record = $('#inPeopleId').datagrid('getChecked');
	if(record==null||record==''){
		$.messager.alert("提示", "请先选择记录");
		return;
	}
	
	var emailIdArr=[];
	for(var i=0;i<record.length;i++){
		emailIdArr[i]=record[i].emailId;
	}
	var emailIdStr=emailIdArr.join(",");
	var emaigroupId=gourp.id;
	reqAddOrMoveArr(emaigroupId,emailIdStr,'/emailGroup/deleteEmailGroupRef');
}

function reqAddOrMove(emailGroupId,emailId,url){
	$.ajax({
		url: url,
		type: 'POST',
		data : {
			emailGroupId:emailGroupId,
            emailId:emailId
		},
		success : function(data) {
			if (data.return_code == '1') {
//				$.messager.alert(market.content.title, "添加成功");
				$("#outPeopleId").datagrid('reload');
				$('#outPeopleId').datagrid('unselectAll');
				$("#inPeopleId").datagrid('reload');
				$('#inPeopleId').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		error : function(e) {
			$.messager.alert("未知错误");
		}
	})
}


function reqAddOrMoveArr(emailGroupId,emailIdStr,url){
	$.ajax({
		url: url,
		type: 'POST',
		data : {
			emailGroupId:emailGroupId,
			emailIdStr:emailIdStr
		},
		success : function(data) {
			if (data.return_code == '1') {
//				$.messager.alert(market.content.title, "添加成功");
				$("#outPeopleId").datagrid('reload');
				$('#outPeopleId').datagrid('unselectAll');
				$("#inPeopleId").datagrid('reload');
				$('#inPeopleId').datagrid('unselectAll');
			} else {
				$.messager.alert(market.content.title, data.return_msg);
			}
		},
		error : function(e) {
			$.messager.alert("未知错误");
		}
	})
}
